package com.ruoyi.newRepair.config;


import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// 自定义类型处理器（处理字符串转数值）
public class DurationTypeHandler extends BaseTypeHandler<Double> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
                                    Double parameter, JdbcType jdbcType) throws SQLException {
        ps.setDouble(i, parameter);
    }

    @Override
    public Double getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String duration = rs.getString(columnName);
        return parseDuration(duration); // 实现字符串转数值逻辑
    }

    @Override
    public Double getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return null;
    }

    @Override
    public Double getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return null;
    }

    private Double parseDuration(String duration) {
        // 示例：假设存储格式为"8小时" -> 转换为8.0
        return Double.parseDouble(duration.replaceAll("[^0-9.]", ""));
    }
}